package com.example.day19;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class musicAdapter extends BaseAdapter {
    List<musicData> datalist;
    Context context;

    public musicAdapter(Context context, List<musicData> datalist){
        this.datalist=datalist;
        this.context=context;
    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder =null;
        if(convertView==null){
            convertView=View.inflate(context,R.layout.list_item,null);
            viewHolder=new ViewHolder();
            viewHolder.name=convertView.findViewById(R.id.music_name);
            viewHolder.person=convertView.findViewById(R.id.person_name);
            viewHolder.time=convertView.findViewById(R.id.music_time);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }
            musicData map =datalist.get(position);
            viewHolder.name.setText(map.getTitle());
            long time = map.getDuration();
            long fen = time/60000;
            long miao = (time%60000)/1000;
            viewHolder.time.setText(fen+"分"+miao+"秒");
            viewHolder.person.setText(map.getArtist());
        return convertView;
    }
    private class ViewHolder{
        TextView name,person,time;
    }
}
